﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Transport;
using Renci.SshNet.Tests.Common;
using System.Globalization;
using System.Linq;

namespace Renci.SshNet.Tests.Classes.Messages.Transport
{
    /// <summary>
    /// Represents SSH_MSG_KEX_DH_GEX_REPLY message.
    /// </summary>
    [TestClass]
    public class KeyExchangeDhGroupExchangeReplyTest : TestBase
    {
        [TestMethod]
        [TestCategory("KeyExchangeInitMessage")]
        [Owner("olegkap")]
        [Description("Validates KeyExchangeInitMessage message serialization.")]
        public void Test_KeyExchangeDhGroupExchangeReply_Load()
        {
            var m = new KeyExchangeDhGroupExchangeReply();
            var input = new byte[] { 0x21, 0x00, 0x00, 0x01, 0x17, 0x00, 0x00, 0x00, 0x07, 0x73, 0x73, 0x68, 0x2d, 0x72, 0x73, 0x61, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0xce, 0x38, 0xdf, 0x0d, 0x04, 0x2c, 0x10, 0x45, 0x8f, 0x5e, 0xeb, 0xc2, 0x35, 0x01, 0xd5, 0x4d, 0x09, 0xa3, 0xee, 0x74, 0x3d, 0x75, 0xcf, 0xf4, 0xf3, 0x4f, 0xc9, 0xf9, 0x2e, 0x59, 0xd6, 0xb8, 0x69, 0x85, 0x16, 0xac, 0x33, 0x5b, 0x2a, 0x2a, 0xf8, 0x9c, 0xc4, 0xc3, 0x87, 0xd6, 0xb8, 0x50, 0x6c, 0xed, 0x60, 0x9c, 0x0f, 0x83, 0x37, 0xb3, 0xa2, 0x25, 0xbe, 0x07, 0xde, 0xb6, 0x1f, 0x28, 0xab, 0x05, 0x21, 0xc0, 0x23, 0xf7, 0xd6, 0xca, 0xcb, 0x03, 0x42, 0x69, 0x31, 0x93, 0x2b, 0x3b, 0x94, 0xb8, 0x3b, 0x90, 0xe6, 0x25, 0x7c, 0x23, 0xc5, 0x24, 0x89, 0x65, 0x37, 0x7b, 0x56, 0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 0xd4, 0x7f, 0x73, 0x76, 0x5d, 0xc2, 0x67, 0xb9, 0xac, 0x8a, 0xd1, 0xab, 0xd4, 0x82 };
            m.Load(input);

            Assert.IsTrue(m.HostKey.SequenceEqual(new byte[] { 0x00, 0x00, 0x00, 0x07, 0x73, 0x73, 0x68, 0x2d, 0x72, 0x73, 0x61, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0xce, 0x38, 0xdf, 0x0d, 0x04, 0x2c, 0x10, 0x45, 0x8f, 0x5e, 0xeb, 0xc2, 0x35, 0x01, 0xd5, 0x4d, 0x09, 0xa3, 0xee, 0x74, 0x3d, 0x75, 0xcf, 0xf4, 0xf3, 0x4f, 0xc9, 0xf9, 0x2e, 0x59, 0xd6, 0xb8, 0x69, 0x85, 0x16, 0xac, 0x33, 0x5b, 0x2a, 0x2a, 0xf8, 0x9c, 0xc4, 0xc3, 0x87, 0xd6, 0xb8, 0x50, 0x6c, 0xed, 0x60, 0x9c, 0x0f, 0x83, 0x37, 0xb3, 0xa2, 0x25, 0xbe, 0x07, 0xde, 0xb6, 0x1f, 0x28, 0xab, 0x05, 0x21, 0xc0, 0x23, 0xf7, 0xd6, 0xca, 0xcb, 0x03, 0x42, 0x69, 0x31, 0x93, 0x2b, 0x3b, 0x94, 0xb8, 0x3b, 0x90, 0xe6, 0x25, 0x7c, 0x23, 0xc5, 0x24, 0x89, 0x65, 0x37, 0x7b, 0x56, 0x54, 0xa2, 0x23, 0xcb, 0xeb, 0xf0, 0xe9, 0xd1, 0x07, 0x88, 0x36, 0x8f, 0xef, 0x65, 0x71, 0xac, 0x80, 0x5c, 0x81, 0x0a, 0x95, 0x1c, 0xf5, 0xc0, 0x5d, 0xd3, 0xd4, 0x10, 0x0e, 0x4f, 0xcf, 0x87, 0x80, 0xa5, 0xde, 0x6e, 0xff, 0x89, 0x9c, 0xfc, 0x2f, 0x85, 0xf5, 0x93, 0xff, 0x70, 0x8e, 0x48, 0xb6, 0x6e, 0x6e, 0x0a, 0x18, 0xa3, 0xbb, 0x1c, 0x56, 0x60, 0xf1, 0x9d, 0x2a, 0xca, 0xbf, 0xa0, 0x84, 0x73, 0xb1, 0x6d, 0x10, 0x75, 0xcc, 0xa3, 0x20, 0xed, 0x00, 0x52, 0xd0, 0x84, 0x5a, 0xc9, 0xbb, 0x17, 0x84, 0x96, 0x40, 0x48, 0xe2, 0x8e, 0x84, 0x30, 0x82, 0x3b, 0x87, 0xa4, 0x54, 0x55, 0x8c, 0xb5, 0x62, 0x97, 0xd7, 0xf1, 0xa3, 0x8b, 0x44, 0xaa, 0x34, 0xfd, 0xe9, 0xad, 0xda, 0xeb, 0x45, 0x27, 0xfb, 0xad, 0xea, 0x9a, 0x0c, 0x38, 0x30, 0x27, 0x6a, 0x82, 0xc2, 0xc2, 0x0a, 0xa0, 0xed, 0x30, 0x2f, 0x50, 0x9f, 0xdb, 0x1a, 0xfb, 0x29, 0x1c, 0x8a, 0x28, 0x3f, 0xd6, 0xf6, 0xa9, 0xeb, 0x76, 0x60, 0xa2, 0x56, 0xa2, 0x90, 0x32, 0xcd, 0x25, 0x41, 0xb7, 0xc0, 0x9e, 0x13, 0x97, 0xaf }));
            Assert.IsTrue(m.Signature.SequenceEqual(new byte[] { 0x00, 0x00, 0x00, 0x07, 0x73, 0x73, 0x68, 0x2d, 0x72, 0x73, 0x61, 0x00, 0x00, 0x01, 0x00, 0x0d, 0x67, 0x83, 0xa1, 0xb8, 0x0c, 0x65, 0xb8, 0xd3, 0x76, 0x5f, 0x5f, 0x05, 0x51, 0x8c, 0xb3, 0x2d, 0x1e, 0x95, 0x67, 0xd6, 0xe2, 0xe4, 0x86, 0x49, 0x11, 0xca, 0x83, 0xab, 0xdb, 0x80, 0x75, 0x4e, 0x89, 0xa7, 0x3d, 0xb6, 0xc2, 0x92, 0xbb, 0x40, 0x00, 0xb9, 0xb3, 0x03, 0x86, 0x19, 0x9d, 0x67, 0x29, 0x2b, 0x5a,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0, 0xd2, 0xb9, 0x3b, 0x83, 0xf4, 0x02, 0xd4, 0x7f, 0x73, 0x76, 0x5d, 0xc2, 0x67, 0xb9, 0xac, 0x8a, 0xd1, 0xab, 0xd4, 0x82 }));
            Assert.IsTrue(m.F.Equals(BigInteger.Parse("139283707184174223973094328642546470115773370503206784462562067731214352325418848176518987606406368553205415627101408281035430394244245366884121855956041981785619368311948822371849754297010084387209814408417993188610908497995240761565618964080251146407679074869975343762660867551322434680855150842669738933549", System.Globalization.NumberStyles.None, CultureInfo.CurrentCulture)));            
        }
    }
}